package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.PracticeBase;
import com.ruoyi.system.service.IPracticeBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基层实践单位信息Controller
 *
 * @author dp
 * @date 2023-11-27
 */
@Api(tags = {"基层实践单位信息"})
@RestController
@RequestMapping("/practice/base")
public class PracticeBaseController extends BaseController {
    @Autowired
    private IPracticeBaseService practiceBaseService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询基层实践单位信息列表
     */
    @ApiOperation(value = "基层实践单位信息列表", notes = "查询基层实践单位信息列表详情", tags = {"基层实践单位信息管理"})
    //@PreAuthorize("@ss.hasPermi('practice:base:list')")
    @GetMapping("/list")
    public TableDataInfo list(PracticeBase practiceBase) {
        startPage();
        List<PracticeBase> list = practiceBaseService.selectPracticeBaseList(practiceBase);
        return getDataTable(list);
    }

    /**
     * 导出基层实践单位信息列表
     */
    @ApiOperation(value = "基层实践单位信息列表导出", notes = "导出基层实践单位信息列表", tags = {"基层实践单位信息管理"})
    //@PreAuthorize("@ss.hasPermi('practice:base:export')")
    @Log(title = "基层实践单位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PracticeBase practiceBase) {
        List<PracticeBase> list = practiceBaseService.selectPracticeBaseList(practiceBase);
        ExcelUtil<PracticeBase> util = new ExcelUtil<PracticeBase>(PracticeBase.class);
        util.exportExcel(response, list, "基层实践单位信息数据");
    }

    /**
     * 获取基层实践单位信息详细信息
     */
    @ApiOperation(value = "基层实践单位信息详细信息", notes = "获取基层实践单位信息详细信息", tags = {"基层实践单位信息管理"})
    //@PreAuthorize("@ss.hasPermi('practice:base:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") Long baseId) {
        return success(practiceBaseService.selectPracticeBaseByBaseId(baseId));
    }

    /**
     * 新增基层实践单位信息
     */
    @ApiOperation(value = "新增基层实践单位信息", notes = "新增基层实践单位信息", tags = {"基层实践单位信息管理"})
    //@PreAuthorize("@ss.hasPermi('practice:base:add')")
    @Log(title = "基层实践单位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody PracticeBase practiceBase) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        practiceBaseService.completeData(practiceBase, userId, 1);
        return toAjax(practiceBaseService.insertPracticeBase(practiceBase));
    }

    /**
     * 修改基层实践单位信息
     */
    @ApiOperation(value = "修改基层实践单位信息", notes = "修改基层实践单位信息", tags = {"基层实践单位信息管理"})
    //@PreAuthorize("@ss.hasPermi('practice:base:edit')")
    @Log(title = "基层实践单位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody PracticeBase practiceBase) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        practiceBaseService.completeData(practiceBase, userId, 1);
        return toAjax(practiceBaseService.updatePracticeBase(practiceBase));
    }

    /**
     * 删除基层实践单位信息
     */
    @ApiOperation(value = "删除基层实践单位信息", notes = "删除基层实践单位信息", tags = {"基层实践单位信息管理"})
    //@PreAuthorize("@ss.hasPermi('practice:base:remove')")
    @Log(title = "基层实践单位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable Long[] baseIds) {
        return toAjax(practiceBaseService.deletePracticeBaseByBaseIds(baseIds));
    }
}
